//
//  PersonSelectTableViewCell.m
//  MobileOffice
//
//  Created by jjkj on 16/12/9.
//  Copyright © 2016年 liaoningjujin. All rights reserved.
//

#import "PersonSelectTableViewCell.h"

@implementation PersonSelectTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier indexPath:(NSIndexPath *)indexPath model:(ManageStrutureModel *)model{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        //self.backgroundColor=COLOR_BACKGROUND_GRAY;
        _model=model;
        _indexPath=indexPath;
        _buttonArr=[NSMutableArray new];
        _myTitle=[MyControl creatLabelWithFrame:CGRectMake(8, 8, IPHONE_WIDTH-16, 20) text:model.itemValue];
        _myTitle.textColor=COLOR_MIDBLUE;
        _myTitle.font=FONT_15;
        [self.contentView addSubview:_myTitle];
    
        //titleArr=@[@"调休",@"探亲假",@"工伤假",@"年假",@"产假",@"丧假",@"婚假",@"事假",@"病假"];
        CGFloat buttonWidth=(IPHONE_WIDTH-15*4)/3;
        for (int i=0; i<model.dataArr.count; i++) {
            ManageStrutureModel *listModel=model.dataArr[i];
            UIButton *signButton=[MyControl creatButtonWithFrame:CGRectMake(15*(i%3+1)+buttonWidth*(i%3), _myTitle.y+_myTitle.height+15*(i/3+1)+30*(i/3), buttonWidth, 30) target:self sel:@selector(dutyClick:) tag:i+200 image:nil title:listModel.itemValue];
            if (listModel.isSelect) {
                signButton.selected=listModel.isSelect;
                signButton.backgroundColor=COLOR_MIDBLUE;
                [signButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            }else{
                signButton.selected=listModel.isSelect;
                signButton.backgroundColor=[UIColor whiteColor];
                [signButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
            }
            signButton.layer.masksToBounds=YES;
            signButton.layer.cornerRadius=5;
            signButton.layer.borderColor=[[UIColor lightGrayColor]CGColor];
            signButton.layer.borderWidth=1.0;
            signButton.titleLabel.font=FONT_12;
            [_buttonArr addObject:signButton];
            [self.contentView addSubview:signButton];
        }
        
        _lineLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, 1)];
        _lineLabel.backgroundColor=BACKCOLOR_GRAY;
        [self.contentView addSubview:_lineLabel];
    }
    return self;
}

- (void)setCellWithManageModel:(ManageStrutureModel *)model indexPath:(NSIndexPath *)indexPath{
    _model=model;
    _indexPath=indexPath;
    CGFloat height=0;
    //如果余数不为0 则要多显示一行
    if (model.dataArr.count%3==0) {
        height=model.dataArr.count/3*(30+15)+15+28;
    }else{
        height=(model.dataArr.count/3+1)*(30+15)+15+28;
    }
    _myTitle.text=model.itemValue;

    _lineLabel.y=height-1;
    //复用问题 所以先将所有button隐藏
    for (UIButton *button in _buttonArr) {
        button.hidden=YES;
    }
    for (int i=0;i<model.dataArr.count; i++) {
        UIButton *signButton=nil;
        ManageStrutureModel *newModel=model.dataArr[i];
        //如果有button则直接使用
        if (i<_buttonArr.count) {
            signButton=_buttonArr[i];
            signButton.hidden=NO;
            [signButton setTitle:newModel.itemValue forState:UIControlStateNormal];
            signButton.tag=200+i;
        }else{
            //如果没有则需要创建
            CGFloat buttonWidth=(IPHONE_WIDTH-15*4)/3;
            signButton=[MyControl creatButtonWithFrame:CGRectMake(15*(i%3+1)+buttonWidth*(i%3), _myTitle.y+_myTitle.height+15*(i/3+1)+30*(i/3), buttonWidth, 30) target:self sel:@selector(dutyClick:) tag:i+200 image:nil title:newModel.itemValue];
            signButton.layer.masksToBounds=YES;
            signButton.layer.cornerRadius=5;
            signButton.layer.borderColor=[[UIColor lightGrayColor]CGColor];
            signButton.layer.borderWidth=1.0;
            signButton.titleLabel.font=FONT_12;
            [_buttonArr addObject:signButton];
            [self.contentView addSubview:signButton];
        }
        if (newModel.isSelect) {
            signButton.selected=newModel.isSelect;
            signButton.backgroundColor=COLOR_MIDBLUE;
            [signButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }else{
            signButton.selected=newModel.isSelect;
            signButton.backgroundColor=[UIColor whiteColor];
            [signButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        }
    }
}

- (void)dutyClick:(UIButton *)button{
    button.selected=!button.selected;
    if (button.selected) {
        button.backgroundColor=COLOR_MIDBLUE;
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        ManageStrutureModel *model=_model.dataArr[button.tag-200];
        model.isSelect=button.selected;
        if ([model.isDepart isEqualToString:@"1"]) {
            if (self.addBlock) {
                self.addBlock(model);
            }
        }else{
            if (self.addPersonBlock) {
                self.addPersonBlock(model);
            }
        }
    }else{
        button.backgroundColor=[UIColor whiteColor];
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        ManageStrutureModel *model=_model.dataArr[button.tag-200];
        model.isSelect=button.selected;
        if ([model.isDepart isEqualToString:@"1"]) {
            if (self.deleteBlock) {
                self.deleteBlock(model);
            }
        }else{
            if (self.deletePersonBlock) {
                self.deletePersonBlock(model);
            }
        }

    }
//    DutyDropListModel *model=_dutyModel.dataArr[button.tag%1000];
//    if (_myBlock) {
//        _myBlock(_dutyType,button.tag%1000,model.keyword,model.BusinessCode);
//    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
